package me.pcqpcq.android.tvlauncher.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

/**
 * 自动滚动文本
 * @author zlh
 * @see http://blog.sina.com.cn/s/blog_821e2bb10100uoao.html
 *
 */
public class AutoScrollTextView extends TextView implements OnClickListener {
	public final static String TAG = AutoScrollTextView.class.getSimpleName();

	/**
	 * 文字长度
	 */
	private float mTextLength = 0f;
	private float mViewWidth = 0f;
	/**
	 * 文字的横坐标
	 */
	private float mStep = 0f;
	/**
	 * 文字的纵坐标
	 */
	private float mYPoint = 0f;
	/**
	 * 用于计算的临时变量
	 */
	private float mOneTextLength = 0.0f;
	/**
	 * 用于计算的临时变量
	 */
	private float mTwoTextLength = 0.0f;
	/**
	 * 是否开始滚动
	 */
	public boolean mIsStarting = false;
	/**
	 * 绘图样式
	 */
	private Paint mPaint = null;
	/**
	 * 文本内容
	 */
	private String mText = "";

	public AutoScrollTextView(Context context) {
		this(context, null);
	}

	public AutoScrollTextView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public AutoScrollTextView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		initView();
	}
	
	@Override
	public void onClick(View v) {
		if (mIsStarting) {
			stopScroll();
		} else {
			startScroll();
		}
	}
	
	@Override
	public void onDraw(Canvas canvas) {
		canvas.drawText(mText, mOneTextLength - mStep, mYPoint, mPaint);
		if (!mIsStarting) {
			return;
		}
		mStep += 0.5;
		if (mStep > mTwoTextLength){
			mStep = mTextLength;
		}
		invalidate();
	}

	@Override
	public Parcelable onSaveInstanceState() {
		Parcelable superState = super.onSaveInstanceState();
		SavedState ss = new SavedState(superState);

		ss.step = mStep;
		ss.isStarting = mIsStarting;

		return ss;

	}

	@Override
	public void onRestoreInstanceState(Parcelable state) {
		if (!(state instanceof SavedState)) {
			super.onRestoreInstanceState(state);
			return;
		}
		SavedState ss = (SavedState) state;
		super.onRestoreInstanceState(ss.getSuperState());

		mStep = ss.step;
		mIsStarting = ss.isStarting;
	}
	
	private void initView() {
		setOnClickListener(this);
	}
	
	public void initParams() {
		mPaint = getPaint();
		mText = getText().toString();
		mTextLength = mPaint.measureText(mText);
		mViewWidth = getWidth();
		mStep = mTextLength;
		mOneTextLength = mViewWidth + mTextLength;
		mTwoTextLength = mViewWidth + mTextLength * 2;
		mYPoint = getTextSize() + getPaddingTop();
	}

	/**
	 * 开始滚动
	 */
	public void startScroll() {
		mIsStarting = true;
		invalidate();
	}

	/**
	 * 停止滚动
	 */
	public void stopScroll() {
		mIsStarting = false;
		invalidate();
	}
	
	/**
	 * 设置画笔颜色，即文字颜色
	 * @param color
	 */
	public void setPaintColor(int color) {
		mPaint.setColor(color);
		invalidate();
	}

	public static class SavedState extends BaseSavedState {
		public boolean isStarting = false;
		public float step = 0.0f;

		SavedState(Parcelable superState) {
			super(superState);
		}

		@Override
		public void writeToParcel(Parcel out, int flags) {
			super.writeToParcel(out, flags);
			out.writeBooleanArray(new boolean[] { isStarting });
			out.writeFloat(step);
		}

		public static final Parcelable.Creator<SavedState> CREATOR = new Parcelable.Creator<SavedState>() {

			public SavedState[] newArray(int size) {
				return new SavedState[size];
			}

			@Override
			public SavedState createFromParcel(Parcel in) {
				return new SavedState(in);
			}
		};

		private SavedState(Parcel in) {
			super(in);
			boolean[] b = null;
			in.readBooleanArray(b);
			if (b != null && b.length > 0)
				isStarting = b[0];
			step = in.readFloat();
		}
	}
}